﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevCore.DB;
using DevCore.Log;

namespace DevFrameWork.Web.DevTest.DevDemo01
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
            //测试完成
            // -- > TestTran();

            //测试sql参数
            //TestParamsOfSql();

            //测试存储过程参数
            TestParamsOfProc();

            //测试完成
            LogHelper.WriteLog("aaaaaaaaaaaaaaaaaa");
            
        }

        private void TestTran()
        {
            SqlHelper dbHelper = new SqlHelper();
            dbHelper.SetTran(true);
            try
            {
                dbHelper.Open();
                dbHelper.BeginTransaction();
                String strSql = "insert into tbsUsers(UserName) values('a')";
                SqlCommand cmd = dbHelper.GetSqlDbCommand(strSql);
                dbHelper.ExecuteNonQuery(cmd);

                //strSql = "insert into tbsusers(InfoId) values('a')";
                //cmd = dbHelper.GetSqlDbCommand(strSql);
                //dbHelper.ExecuteNonQuery(cmd);
                dbHelper.CommitTransaction();
            }
            catch (Exception ex)
            {
                dbHelper.RollbackTransaction();
            }





            dbHelper.Close();
        }

        private void TestParamsOfSql()
        {
            SqlHelper dbHelper = new SqlHelper();
            dbHelper.SetTran(true);
            try
            {
                dbHelper.Open();
                dbHelper.BeginTransaction();
                String strSql = "insert into tbsUsers(UserName,UserPassword) values(@UserName,@UserPassword)";
                SqlCommand cmd = dbHelper.GetSqlDbCommand(strSql);
                List<DbParamInfo> dicParams = new List<DbParamInfo>();
                dicParams.Add(new DbParamInfo() { Name = "UserName", Value = "bbb" });
                dicParams.Add(new DbParamInfo() { Name = "UserPassword", Value = "bbb" });
                dbHelper.AddSqlParam(cmd, dicParams);
                dbHelper.ExecuteNonQuery(cmd);

                //strSql = "insert into tbsusers(InfoId) values('a')";
                //cmd = dbHelper.GetSqlDbCommand(strSql);
                //dbHelper.ExecuteNonQuery(cmd);
                dbHelper.CommitTransaction();
            }
            catch (Exception ex)
            {
                dbHelper.RollbackTransaction();
            }





            dbHelper.Close();
        }

        private void TestParamsOfProc()
        {
            SqlHelper dbHelper = new SqlHelper();
            dbHelper.SetTran(true);
            try
            {
                dbHelper.Open();
                dbHelper.BeginTransaction();
                //String strSql = "insert into tbsUsers(UserName,UserPassword) values(@UserName,@UserPassword)";
                String procName = "Test_App_spSaveUser";
                SqlCommand cmd = dbHelper.GetProcDbCommand(procName);
                List<DbParamInfo> dicParams = new List<DbParamInfo>();

                dicParams.Add(new DbParamInfo() { Name = "UserName", Value = "aa", DbType = "VarChar", Direction = 0, Size = 50 });
                dicParams.Add(new DbParamInfo() { Name = "UserPassword", Value = "UserPassword", DbType = "VarChar", Direction = 0, Size = 50 });
                dicParams.Add(new DbParamInfo() { Name = "Msg", Value = "", DbType = "VarChar", Direction = 1, Size = 200 });
                
                dbHelper.AddProcParameters(cmd, dicParams);
                dbHelper.ExecuteNonQuery(cmd);

                String outMsg = cmd.Parameters["@po_Msg"].Value.ToString();
                Response.Write(outMsg);

                dbHelper.CommitTransaction();
            }
            catch (Exception ex)
            {
                dbHelper.RollbackTransaction();
            }





            dbHelper.Close();
        }
    }
}
